﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PhatPhucLoc.common;
using PhatPhucLoc.dao;
using PhatPhucLoc.model;
using PhatPhucLoc.report;
using PhatPhucLoc.Model;
using System.Collections;

namespace PhatPhucLoc.view
{
    public partial class FormXuatBan : Form
    {
        public DataTable dtHH;
        public FormXuatBan()
        {
            InitializeComponent();
        }

        private void FormXuatBan_Load(object sender, EventArgs e)
        {
            comboBox1.Enabled = false;
            button1.Enabled = false;
            txtSoluong.Enabled = false;
            LoadKH();

            dtHH = new DataTable();
            dtHH.Columns.Add("ten", typeof(String));
            dtHH.Columns.Add("soluong", typeof(String));
            dtHH.Columns.Add("dongia", typeof(String));
            dtHH.Columns.Add("thanhtien", typeof(String));
            dtHH.Columns.Add("mahang", typeof(String));

            DataColumn[] PrimaryKey = new DataColumn[1];
            PrimaryKey[0] = dtHH.Columns["mahang"];

            dtHH.PrimaryKey = PrimaryKey;
        }

        private void LoadKH()
        {
            DataTable dt = KhachHangDAO.KhachHang_GetAll();
            cbbKhachHang.DataSource = dt;
            cbbKhachHang.DisplayMember = "Ten";
            cbbKhachHang.ValueMember = "ID";

        }

        private void btnXuatBan_Click(object sender, EventArgs e)
        {
            comboBox1.Enabled = true;
            button1.Enabled = true;
            cbbKhachHang.Enabled = false;
            txtSoluong.Enabled = true;
            LoadHH();
        }

        private void LoadHH()
        {
            DataTable dt = KhachHang_HangHoaDAO.GetAll_ByID(cbbKhachHang.SelectedValue.ToString());
            comboBox1.ValueMember = "id";
            comboBox1.DisplayMember = "ten";
            comboBox1.DataSource = dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string id = comboBox1.SelectedValue.ToString();
            int soluong = int.Parse(txtSoluong.Text);
            float thanhtien = soluong * float.Parse(textBox1.Text);
            float tongtien = float.Parse(txtTongtien.Text) + thanhtien;
            txtTongtien.Text = tongtien.ToString();

            DataRow row = dtHH.NewRow();
            row["ten"] = comboBox1.Text;
            row["soluong"] = txtSoluong.Text;
            row["dongia"] = textBox1.Text;
            row["thanhtien"] = thanhtien;
            row["mahang"] = comboBox1.SelectedValue;

            try
            {
                dtHH.Rows.Add(row);
            }
            catch (ConstraintException)
            {
                DataRow[] rows = dtHH.Select("mahang = '" + comboBox1.SelectedValue+"'");
                int soluong1 = int.Parse(rows[0]["soluong"].ToString()) + soluong;
                rows[0]["soluong"] = soluong1;
                rows[0]["thanhtien"] = float.Parse(rows[0]["dongia"].ToString()) * soluong1;
                dtHH.AcceptChanges();
            }
            dataGridView1.DataSource = dtHH;

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = KhachHang_HangHoaDAO.GetPrice_ByID(cbbKhachHang.SelectedValue.ToString(), comboBox1.SelectedValue.ToString());
            textBox1.Text = dt.Rows[0][0].ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ArrayList ds = new ArrayList();
            HoaDonModel model = new HoaDonModel();
            model.MaKhachHang = cbbKhachHang.SelectedValue.ToString();
            model.TongTien = txtTongtien.Text;

            string hoadonID = HoaDonDAO.Insert(model);

            foreach (DataRow row in dtHH.Rows)
            {
                ChiTietHoaDonModel ctModel = new ChiTietHoaDonModel();
                ctModel.MaHoaDon = hoadonID;
                ctModel.MaSanPham = row["mahang"].ToString();
                ctModel.GiaTien = row["dongia"].ToString();
                ctModel.SoLuong = row["soluong"].ToString();

                ChiTietHoaDonDAO.Insert(ctModel);

                hoadonReport rp = new hoadonReport();
                rp.Dongia = int.Parse(row["dongia"].ToString());
                rp.Sohoadon = hoadonID;
                rp.Ngaylap = DateTime.Now;
                rp.Mahang = row["mahang"].ToString();
                rp.Soluong = int.Parse(row["soluong"].ToString());
                rp.Tenhang = row["ten"].ToString();
                rp.Tenkhachhang = cbbKhachHang.Text;
                rp.Thanhtien = double.Parse(row["thanhtien"].ToString());
                rp.Tongtienchu = "";

                ds.Add(rp);
            }

            frmReportHD frm = new frmReportHD();
            frm.dsHD = ds;
            frm.Show();
            ds.Clear();
            //MessageBox.Show("Da them thong tin hoa don");

            DataTable dt = this.dtHH;
            //FormReport frm = new FormReport(dt);
            //frm.ShowDialog();

        }
    }
}
